# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  #protect_from_forgery # See ActionController::RequestForgeryProtection for details

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
  
  @@title = "ระบบการรวบรวมผลผลิตทางการเกษตรของสหกรณ์และกลุ่มเกษตรกร : "
  @@per_page = 0
  @@per_page_user = 30
  @@per_page_coop = 100
  @@per_page_plant_data = 100
  
  def login_required
    if session[:user]
      return true
    end

    redirect_to :controller => "user_gdi07", :action => "login"
    return false
  end  

  def getdate
    month_th = {"01" => "มกราคม",
                "02" => "กุมภาพันธ์",
                "03" => "มีนาคม",
                "04" => "เมษายน",
                "05" => "พฤษภาคม",
                "06" => "มิถุนายน",
                "07" => "กรกฎาคม",
                "08" => "สิงหาคม",
                "09" => "กันยายน",
                "10" => "ตุลาคม",
                "11" => "พฤศจิกายน",
                "12" => "ธันวคม"}
    
    t = Time.now
    d = t.strftime("%d")
    d = d.to_i
    d = d.to_s
    m = month_th[t.strftime("%m")]  
    y = t.strftime("%Y")
    y = y.to_i + 543
    y = y.to_s
    
    return "วันที่ " + d + " " + m + " พ.ศ." + y
  end
  
  def relation_users
    sql = "INNER JOIN province_gdi07s ON user_gdi07s.province_id = province_gdi07s.id"
    return sql 
  end

  def relation_coops
    sql  = "INNER JOIN coop_type_gdi07s ON coop_gdi07s.coop_type_code = coop_type_gdi07s.coop_type_code "
    sql << "INNER JOIN province_gdi07s ON coop_gdi07s.province_id = province_gdi07s.id"
    return sql 
  end

  def relation_plant_datas
    sql  = "INNER JOIN coop_gdi07s ON plant_data_gdi07s.coop_code = coop_gdi07s.coop_code "
    sql << "INNER JOIN coop_type_gdi07s ON coop_gdi07s.coop_type_code = coop_type_gdi07s.coop_type_code "
    sql << "INNER JOIN province_gdi07s ON coop_gdi07s.province_id = province_gdi07s.id"
    return sql
  end
  
  def chk_email(email)
    if email =~ /(.+)@(.+)\.(.{3})/
      return true
    else  
      return false  
    end
  end  

  def chk_session(menu)
    if session[:user]
      result = UserGdi07.find(:first, :joins => relation_users, :conditions => ["user_gdi07s.id = ? AND user_gdi07s.username = ? AND user_gdi07s.password = ? AND user_gdi07s.province_id = ? AND user_gdi07s.user_type = ?",session[:user][:id], session[:user][:username], session[:user][:password], session[:user][:province_id], session[:user][:user_type]])      
      if (result == nil) || ((session[:user][:username] <=> result.username) != 0) || ((session[:user][:password] <=> result.password) != 0)
        redirect_to :controller => "user_gdi07", :action => "signout"
        return false
      else
        if (menu == 1) && ((session[:user][:menu1] <=> "Y") != 0)
          redirect_to :controller => "user_gdi07", :action => "signout"
          return false
        end

        if (menu == 2) && ((session[:user][:menu2] <=> "Y") != 0)
          redirect_to :controller => "user_gdi07", :action => "signout"
          return false
        end

        if (menu == 3) && ((session[:user][:menu3] <=> "Y") != 0)
          redirect_to :controller => "user_gdi07", :action => "signout"
          return false
        end

        return true
      end
    end
  end  

   def page(current_page,record_count,model)
    result = []
   
    if record_count > 0
      case model
        when "user"
          @@per_page = @@per_page_user
        when "coop"
          @@per_page = @@per_page_coop
        when "plant_data"
          @@per_page = @@per_page_plant_data
      end    
      num_page = 1;
      if record_count > @@per_page
        if (record_count % @@per_page) == 0
          num_page = (record_count / @@per_page) 
        else 
          num_page = (record_count / @@per_page) + 1
        end
      end
      
      current_page = current_page.to_i
      current_page = (current_page == nil) || (current_page < 1) ? 1 : (current_page > num_page ? num_page : current_page)

      if current_page == 1
        row_start = 0;
      else
        row_start = (current_page * @@per_page) - @@per_page;
      end
    else
      num_page = 0
      current_page = 0
      row_start = 0
    end
    result[0] = num_page
    result[1] = current_page
    result[2] = row_start
    
    return result
  end  
end